package com.springcloud.zuul.common.utlis;

import com.alibaba.fastjson.JSON;
import com.springcloud.zuul.common.constant.ServiceCode;
import com.springcloud.zuul.common.constant.ServiceErrCode;
import org.apache.commons.collections.MapUtils;

import java.io.Serializable;
import java.util.HashMap;


public class JsonResult<T> extends HashMap<String, Object> implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final String SUCCESSCODE = "200";
    private static final String SUCCESSMSG = "ok";
    private static final String FAILEDCODE = "400";
    private static final String FAILEDMSG = "服务器繁忙，请稍后重试！";

    private static final String RESULT = "result";
    private static final String ERROR_CODE = "errorCode";
    private static final String ERROR_MESSAGE = "errorMessage";

    /**
     * Description:返回结果集, 无记录条数
     * @author zhlu
     * @date 2019/12/25 7:17 下午
     * @param object
     * @param errCode
     * @param errMsg
     * @return com.springcloud.zuul.common.utlis.JsonResult
     */
    public JsonResult put(Object object, String errCode, String errMsg) {
        super.put(RESULT, object);
        super.put(ERROR_CODE, errCode);
        super.put(ERROR_MESSAGE, errMsg);
        return this;
    }

    /**
     * Description:返回结果集
     * @author zhlu
     * @date 2019/12/25 7:17 下午
     * @param data
     * @return com.springcloud.zuul.common.utlis.JsonResult
     */
    public static JsonResult success(Object data) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.superPut(RESULT, data);
        jsonResult.superPut(ERROR_CODE, JsonResult.SUCCESSCODE);
        jsonResult.superPut(ERROR_MESSAGE, JsonResult.SUCCESSMSG);
        return jsonResult;
    }

    /**
     * Description:返回结果集
     * @author zhlu
     * @date 2019/12/25 7:17 下午
     * @param data
     * @return com.springcloud.zuul.common.utlis.JsonResult
     */
    public static JsonResult success(Object data,ServiceCode serviceCode) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.superPut(RESULT, data);
        jsonResult.superPut(ERROR_CODE, serviceCode.getCode());
        jsonResult.superPut(ERROR_MESSAGE, serviceCode.getMsg());
        return jsonResult;
    }

    /**
     * Description:返回结果集
     * @author zhlu
     * @date 2019/12/25 7:17 下午
     * @param data
     * @return com.springcloud.zuul.common.utlis.JsonResult
     */
    public static JsonResult success(Object data,ServiceErrCode serviceErrCode) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.superPut(RESULT, data);
        jsonResult.superPut(ERROR_CODE, serviceErrCode.getCode());
        jsonResult.superPut(ERROR_MESSAGE, serviceErrCode.getMsg());
        return jsonResult;
    }

    /**
     * Description:返回结果集
     * @author zhlu
     * @date 2019/12/26 2:21 下午
     * @param errorCode
     * @param errorMessage
     * @return com.springcloud.zuul.common.utlis.JsonResult
     */
    public static JsonResult success(String errorCode,String errorMessage) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.superPut(ERROR_CODE, errorCode);
        jsonResult.superPut(ERROR_MESSAGE, errorMessage);
        return jsonResult;
    }

    /**
     * Description:判断是否成功
     * @author zhlu
     * @date 2019/12/25 7:18 下午
     * @param
     * @return boolean
     */
    public boolean isSuccess() {
        String code = (String) this.get(ERROR_CODE);
        return code.equals(SUCCESSCODE);
    }

    /**
     * Description:返回错误异常
     * @author zhlu
     * @date 2019/12/25 7:26 下午
     * @param serviceCode
     * @return com.springcloud.zuul.common.utlis.JsonResult
     */
    public static JsonResult error(ServiceCode serviceCode) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.superPut(ERROR_CODE, serviceCode.getCode());
        jsonResult.superPut(ERROR_MESSAGE, serviceCode.getMsg());
        return jsonResult;
    }

    /**
     * Description:返回错误异常
     * @author zhlu
     * @date 2019/12/25 7:26 下午
     * @param serviceErrCode
     * @return com.springcloud.zuul.common.utlis.JsonResult
     */
    public static JsonResult error(ServiceErrCode serviceErrCode) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.superPut(ERROR_CODE, serviceErrCode.getCode());
        jsonResult.superPut(ERROR_MESSAGE, serviceErrCode.getMsg());
        return jsonResult;
    }

    /**
     * Description:返回错误异常
     * @author zhlu
     * @date 2019/12/25 7:27 下午
     * @param errorCode
     * @param errorMsg
     * @return com.springcloud.zuul.common.utlis.JsonResult
     */
    public static JsonResult error(String errorCode,String errorMsg) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.superPut(ERROR_CODE, errorCode + "");
        jsonResult.superPut(ERROR_MESSAGE, errorMsg);
        return jsonResult;
    }

    /**
     * Description:返回错误的信息
     * @author zhlu
     * @date 2019/12/25 7:28 下午
     * @param
     * @return java.lang.String
     */
    public String getErrorMessage() {
        return MapUtils.getString(this, ERROR_MESSAGE);
    }

    /**
     * Description:返回错误的代码
     * @author zhlu
     * @date 2019/12/25 7:28 下午
     * @param
     * @return int
     */
    public int getErrorCode() {
        return MapUtils.getIntValue(this, ERROR_CODE);
    }

    /**
     * 返回数据
     *
     * @return
     */
    public T getResult() {
        return (T) this.get(RESULT);
    }

    /**
     * Description:返回结果集, 带记录条数
     * @author zhlu
     * @date 2019/12/25 7:29 下午
     * @param object
     * @param num
     * @return com.springcloud.zuul.common.utlis.JsonResult
     */
    public JsonResult put(Object object, String num) {
        super.put(RESULT, object);
        super.put(ERROR_CODE, SUCCESSCODE);
        super.put(ERROR_MESSAGE, SUCCESSMSG);
        super.put("total", num);
        return this;
    }

    /**
     * 返回成功/错误信息+errCode
     * @param errMsg  结果集
     * @param errCode errorcode
     * @return
     */
    public JsonResult put(String errMsg, String errCode) {
        super.put(ERROR_CODE, errCode);
        super.put(ERROR_MESSAGE, errMsg);
        return this;
    }


    private void superPut(String key, Object val) {
        super.put(key, val);
    }

    /**
     * Description:返回成功信息+errCode
     * @author zhlu
     * @date 2019/12/25 7:31 下午
     * @param
     * @return com.springcloud.zuul.common.utlis.JsonResult
     */
    public JsonResult putSuccess() {
        super.put(ERROR_CODE, SUCCESSCODE);
        super.put(ERROR_MESSAGE, SUCCESSMSG);
        return this;
    }

    public String toJson() {
        return JSON.toJSONString(this);
    }

    /**
     * Description:返回错误
     * @author zhlu
     * @date 2019/12/25 7:49 下午
     * @param errorMsg
     * @return com.springcloud.zuul.common.utlis.JsonResult
     */
    public static JsonResult error(String errorMsg) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.superPut(ERROR_CODE, JsonResult.FAILEDCODE);
        jsonResult.superPut(ERROR_MESSAGE, errorMsg);
        return jsonResult;
    }

    /**
     * Description:返回错误
     * @author zhlu
     * @date 2019/12/25 7:49 下午
     * @return com.springcloud.zuul.common.utlis.JsonResult
     */
    public static JsonResult error() {
        JsonResult jsonResult = new JsonResult();
        jsonResult.superPut(ERROR_CODE, JsonResult.FAILEDCODE);
        jsonResult.superPut(ERROR_MESSAGE, JsonResult.FAILEDMSG);
        return jsonResult;
    }

    public static JsonResult success() {
        return JsonResult.success(SUCCESSCODE,SUCCESSMSG);
    }

    public static JsonResult actionSuccess() {
        return JsonResult.success(SUCCESSCODE,SUCCESSMSG);
    }

    public JsonResult<T> put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
